package com.net.xpay.stat.scheduler;


import com.net.xpay.stat.task.UserDailyStatDailyTask;
import com.net.xpay.common.helper.NoticeHelper;
import com.net.xpay.common.service.UserDailyStatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 2019/7/15 16:28

 * 用户经营数据日统计
 */
@Slf4j
@Component
public class UserDailyStatScheduler {

    @Autowired
    private UserDailyStatDailyTask userDailyStatDailyTask;

    @Autowired
    private NoticeHelper noticeHelper;

    @Autowired
    private UserDailyStatService userDailyStatService;

    /**
     * 更新用户经营数据日统计
     */
    @Scheduled(cron = "0 5 0 * * ?")
    public void doUserDailyStat() {
        try {
            long t1 = System.currentTimeMillis();
            log.info("start schedule doUserDailyStat|now={}", LocalDateTime.now());
            userDailyStatDailyTask.startDailyWork();
            long t2 = System.currentTimeMillis();
            log.info("end schedule doUserDailyStat|now={}|cost={}", LocalDateTime.now(), t2 - t1);
            noticeHelper.sendSystemInfoNotice("用户经营数据日统计生成完成，耗费时间：" + (t2 - t1), "text");
        } catch (Throwable e) {
            log.warn("doUserDailyStat error", e);
            noticeHelper.sendSystemMonitorNotice("用户经营数据日统计失败|doUserDailyStat error", "text");
        }
    }

}
